package com.freewheelycheckin;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.support.v4.app.NavUtils;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class AccountList extends Activity  {

	protected AccountManager accountManager= null;
    protected Intent intent=null;
    
	/** Called when the activity is first created. */
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_account_list);
	    this.setTitle(R.string.account_list_title);
	    accountManager = AccountManager.get(getApplicationContext());
	    final Account[] accounts = accountManager.getAccountsByType("com.google");
	    final ListView listview = (ListView) findViewById(R.id.listview);
	    //store activity for use in callback
	    final Activity accountListActivity= this;
	    final ArrayList<String> accountNames = new ArrayList<String>();
	    for (int i = 0; i < accounts.length; ++i) {
	    	accountNames.add(accounts[i].name);
	    }
	    //add click listener
	    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, final View view,
			          int position, long id) {
				 	Account account = accounts[position];
		            Intent intent = new Intent(accountListActivity, LocationLogger.class);
		            intent.putExtra("account", account);
		            startActivity(intent);
				
			}
	    	
	    });
	    if(accounts.length>0){
	    	listview.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, accountNames));        
	    }
	}

   

}
